// pages/my/replaceProduct/replaceStatistics.js
import moment from '../../../utils/moment.js'
var app = getApp()

Page({
  data: {
  	orderTab:0,
  	agentId:'',
  	accountId:'',
  	newGoodsList:[],
  	oldGoodsList:[],
    orderPrice:[],
    sales_man:[],
    orderNo:[],
    orderIds:[],
    blackPrice:true,
    actualPriceShow:true,
    goodsInfoShow:true,
    cash:0,
    cash_in:0,
    assets:0
  },
  onLoad: function (options) {
  	app.getMemInfo().then(res=>{
  		console.log()
  		this.setData({
  			agentId:res.data.agentId,
  			accountId:res.data.id
  		})
  		this.getOrderList(res.data.id,1)
      this.getCashier(res.data.id)
  	})
  },
  onShow: function () {
    app.getMemInfo().then(res=>{
      this.getOrderList(res.data.id,1)
    })
    this.setData({
      orderTab:0
    })
  },
  changeType(e){
    let status = e.currentTarget.dataset.paystatus
    this.setData({
      orderTab:status
    })
    if (status==0) {
      this.getOrderList(this.data.accountId,1)
    }else{
      this.getOrderList(this.data.accountId,2)
    }
  },
  getOrderList(accountId,orderPayState){
    wx.showLoading({
      title:'正在加载'
    })
  	let that = this
  	let data = {
  		accountId,
      orderPayState,//1待支付 2已支付
      isOldForNew:1
  	}
  	console.log(data)
  	app.request('wx_app/user_order/list',data,'GET').then(res=>{
  		console.log(res)
  		if (res.code==1) {
    		let newGoodsList = [],
    		    oldGoodsList = [],
            orderPrice = [],
            sales_man = [],
            orderNo = [],
            orderIds = []
        if (res.data.orders) {
  	  		res.data.orders.forEach(item=>{
            let temporary1 = []
            let temporary2 = []
            //获取新商品列表
            item.orderDetails.forEach(_item=>{
              temporary1.push({
                name:_item.name,
                num:_item.amount,
                actualPrice:_item.actualPrice,
                price:_item.amount,
                type:_item.des,
                url:_item.pic,
                id:_item.productId,
                color:_item.tagColor,
                companyName:_item.productTag
              })
            })
            //获取旧商品列表
            item.oldSkus.forEach(_item=>{
              temporary2.push({
                name:_item.productName,
                url:_item.imgUrl1,
                num:_item.amount,
                price:_item.stockPrice,
                type:_item.versionValues,
                id:_item.id,
                color:_item.tagColor,
                companyName:_item.productTag
              })
            })
            newGoodsList.push(temporary1)
            oldGoodsList.push(temporary2)
            orderPrice.push(item.orderPrice)
            sales_man.push(item.sales_man)
            orderNo.push(item.orderNo)
            orderIds.push(item.orderId)
  	  		})
        }else{
          wx.hideLoading()
          app.errorMsg('没有订单')
        }
        that.setData({
          newGoodsList,
          oldGoodsList,
          orderPrice,
          sales_man,
          orderNo,
          orderIds
        })
        wx.hideLoading()
  		}else{
        wx.hideLoading()
        app.errorMsg(res.message)
  		}
  	})
  },
  getOrderDetail(e){
    let type = ''
    if (this.data.orderTab==0) {
      type=1
    }else{
      type=2
    }
    wx.navigateTo({
      url: 'replacePay?orderId='+e.currentTarget.dataset.id+'&type='+type
    })
  },
  productList(){
    wx.redirectTo({
      url:'replaceProductList'
    })
  },
  getCashier(accountId){
    let data = {
      accountId,
      startTime:moment().format("YYYY-MM-DD"),
      endTime:moment().format("YYYY-MM-DD"),
    }
    console.log(data)
    app.request('wx_app/oldForNew/cashier',data,'GET').then(res=>{
      console.log(res)
      if (res.code==1) {
        this.setData({
          cash:res.data.cash,
          cash_in:res.data.cash_in,
          assets:res.data.assets
        })
      }else{
        app.errorMsg(res.message)
      }
    })
  },
  bindTurnover(){
    wx.navigateTo({
      url:'../turnover/turnover?type=1'
    })
  }
})
